package easyits.ws;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;


/**
 * <!-- axis2 -->
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-kernel</artifactId>
			<version>1.7.4</version>
			<exclusions>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-codegen</artifactId>
			<version>1.7.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-adb</artifactId>
			<version>1.7.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-transport-local</artifactId>
			<version>1.7.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-transport-http</artifactId>
			<version>1.7.4</version>
		</dependency>
		<!-- axis2end -->
 * @author Administrator
 * webservice接口处理类
 */
public class WsUtil {
	private Hearder hearder;
	public WsUtil(Hearder hearder){
		super();
		this.hearder=hearder;
	}
	private OMElement om=null;
    /**
     * 默认是为西交投的ws
     * @param hearder
     * @param args
     * @return
     */
    public  OMElement excute(Map<String,String> args){
   	 	Options options = new Options();  
        options.setAction(hearder.getAction());// 调用接口方法  
        options.setTo(new EndpointReference(hearder.getEpr()));  
        options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制. 
        try {
       	 ServiceClient sender  = new ServiceClient();
			sender.setOptions(options);  
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs =fac.createOMNamespace(hearder.getTargetNameSpace(), "");
			OMElement method = fac.createOMElement(hearder.getMethod(), omNs);
			Set<Entry<String, String>>sets=args.entrySet();
			for (Entry<String,String> it : sets) {
				OMElement name = fac.createOMElement(it.getKey(), omNs);// 设置入参名称  
				name.setText(it.getValue());
				method.addChild(name); 
			}
			method.build();
			OMElement response = sender.sendReceive(method);
			//T object = (T) BeanUtil.processObject(response, cls, null, true, new DefaultObjectSupplier(),null);
			return response;
		} catch (Exception e) {
			System.out.println("Error");  
			e.printStackTrace();
		} 
        return null;
    }
    
    
    public static void main(String[] args) throws AxisFault {
    	Hearder hearder2=new Hearder("GetCarDriverInfo");
    	WsUtil ws =new WsUtil(hearder2);
    /*	
    	Hearder hearder=new Hearder("ValidUserInfo");
    	Map<String,String> arg= new HashMap<String, String>();
    	arg.put("username", "sxdh");
    	arg.put("password", "sxdh");
    	arg.put("version", "v1.0.0");
    	ValidUserInfo m =ws.excute(hearder, Argo, ValidUserInfo.class);
    	System.out.println(m);*/
    	
    	Map<String,String> m2= new HashMap<String, String>();
    	m2.put("carno", "U8222");
    	m2.put("idcard", "610121197006216279");
    	m2.put("start", "1945");
    	m2.put("end", "2088");
    	m2.put("driverName", "");
    	m2.put("conum", "99999");
    	m2.put("version", "v1.0.0");    	
    	ws.excute( m2);
    }
    private  synchronized  void getTheNodeValue(OMElement in, String nodeName)
    {
        try
        {
            Iterator desc = in.getChildElements();
            while (desc.hasNext())
            {
                OMElement element = (OMElement) desc.next();
                if(element.getChildElements().hasNext()){
                	getTheNodeValue(element, nodeName);
                }
//                OMElement e = element.getFirstChildWithName(new QName(nodeName));
                if (element.getLocalName().equals(nodeName))
                {
                	om=element;
                	return;
                }
            }
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
    }
    public OMElement getInnerNode(OMElement in, String nodeName){
    	getTheNodeValue(in, nodeName);
		return om;
    }
}
